package com.silence.springboot.service.impl;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.silence.springboot.convert.ReturnSchoolInfoConvert;
import com.silence.springboot.entity.ReturnSchoolInfoEntity;
import com.silence.springboot.mapper.ReturnSchoolInfoMapper;
import com.silence.springboot.service.ReturnSchoolInfoService;
import com.silence.springboot.vo.input.PageReturnSchoolInfoInput;
import com.silence.springboot.vo.input.SaveReturnSchoolInfoInput;
import com.silence.springboot.vo.output.PageReturnSchoolInfoOutput;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.silence.springboot.entity.table.ReturnSchoolInfoEntityTableDef.RETURN_SCHOOL_INFO_ENTITY;

/**
 * 学生返校信息表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ReturnSchoolInfoServiceImpl extends ServiceImpl<ReturnSchoolInfoMapper, ReturnSchoolInfoEntity> implements ReturnSchoolInfoService {

    /**
     * 转换器
     */
    private final ReturnSchoolInfoConvert returnSchoolInfoConvert;

    /**
     * 分页查询
     *
     * @param input 入参
     * @return 分页结果
     */
    @Override
    public Page<PageReturnSchoolInfoOutput> pageInfo(PageReturnSchoolInfoInput input) {

        Page<ReturnSchoolInfoEntity> page = Page.of(input.getPageNum(), input.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(RETURN_SCHOOL_INFO_ENTITY.ALL_COLUMNS)
                .from(RETURN_SCHOOL_INFO_ENTITY)
                .where(RETURN_SCHOOL_INFO_ENTITY.NAME.like(input.getName(), StringUtils::isNotBlank))
                .where(RETURN_SCHOOL_INFO_ENTITY.STUDENT_NUMBER.eq(input.getStudentNumber(), StringUtils::isNotBlank))
                .where(RETURN_SCHOOL_INFO_ENTITY.PHONE.eq(input.getPhone(), StringUtils::isNotBlank));
        if (StringUtils.isNotBlank(input.getReturnStartDate()) && StringUtils.isNotBlank(input.getReturnEndDate())) {
            LocalDate startDate = LocalDateTimeUtil.parseDate(input.getReturnStartDate());
            LocalDate endDate = LocalDateTimeUtil.parseDate(input.getReturnEndDate());
            LocalDateTime startTime = startDate.atTime(0, 0, 0);
            LocalDateTime endTime = endDate.atTime(23, 59, 59);
            queryWrapper.where(RETURN_SCHOOL_INFO_ENTITY.RETURN_DATE.between(startTime, endTime));
        }
        return returnSchoolInfoConvert.convertPageReturnSchoolInfoOutput(page(page, queryWrapper));
    }

    /**
     * 保存返校信息
     *
     * @param input 入参
     */
    @Override
    public void saveInfo(SaveReturnSchoolInfoInput input) {
        save(returnSchoolInfoConvert.convertReturnSchoolInfoEntity(input));
    }
}